from sqlalchemy.orm import Session, joinedload
from sqlalchemy import and_, func
from typing import List, Optional
from datetime import datetime, date

from models.models import LearnknowledgeCard
from schemas.knowledge_card import KnowledgeCardCreate, KnowledgeCardUpdate

# 创建知识卡片
def create_knowledge_card(db: Session, card: KnowledgeCardCreate, user_id: int) -> LearnknowledgeCard:
    db_card = LearnknowledgeCard(
        user_id=user_id,
        material_id=card.material_id,
        title=card.title,
        summary=card.summary,
        html_content=card.html_content,
        page_number=card.page_number
    )
    db.add(db_card)
    db.commit()
    db.refresh(db_card)
    return db_card

# 获取单个知识卡片
def get_knowledge_card(db: Session, card_id: int, user_id: int) -> Optional[LearnknowledgeCard]:
    return db.query(LearnknowledgeCard).filter(
        and_(
            LearnknowledgeCard.id == card_id,
            LearnknowledgeCard.user_id == user_id
        )
    ).first()

# 获取用户的所有知识卡片
def get_user_knowledge_cards(
    db: Session, 
    user_id: int, 
    material_id: Optional[int] = None,
    skip: int = 0, 
    limit: int = 100
) -> List[LearnknowledgeCard]:
    query = db.query(LearnknowledgeCard).options(joinedload(LearnknowledgeCard.material)).filter(LearnknowledgeCard.user_id == user_id)
    
    if material_id is not None:
        query = query.filter(LearnknowledgeCard.material_id == material_id)
    
    return query.offset(skip).limit(limit).all()

# 更新知识卡片
def update_knowledge_card(
    db: Session,
    card_id: int,
    user_id: int,
    card_update: KnowledgeCardUpdate
) -> Optional[LearnknowledgeCard]:
    db_card = get_knowledge_card(db, card_id, user_id)
    if not db_card:
        return None
    
    update_data = card_update.dict(exclude_unset=True)
    for field, value in update_data.items():
        setattr(db_card, field, value)
    
    db.commit()
    db.refresh(db_card)
    return db_card

# 删除知识卡片
def delete_knowledge_card(db: Session, card_id: int, user_id: int) -> bool:
    db_card = get_knowledge_card(db, card_id, user_id)
    if not db_card:
        return False
    
    db.delete(db_card)
    db.commit()
    return True

# 获取特定材料的知识卡片数量
def get_knowledge_cards_count(db: Session, material_id: int, user_id: int) -> int:
    return db.query(LearnknowledgeCard).filter(
        and_(
            LearnknowledgeCard.material_id == material_id,
            LearnknowledgeCard.user_id == user_id
        )
    ).count()

# 获取今日创建的知识卡片
def get_today_knowledge_cards(
    db: Session,
    user_id: int,
    material_id: Optional[int] = None
) -> List[LearnknowledgeCard]:
    today = date.today()
    query = db.query(LearnknowledgeCard).filter(
        and_(
            LearnknowledgeCard.user_id == user_id,
            func.date(LearnknowledgeCard.created_at) == today
        )
    )
    
    if material_id is not None:
        query = query.filter(LearnknowledgeCard.material_id == material_id)
    
    return query.order_by(LearnknowledgeCard.created_at.desc()).all()
